package com.wwj.core.wawa.service.impl;

import com.wwj.core.wawa.entity.PlayerAddress;
import com.wwj.core.wawa.entity.mapper.PlayerAddressMapper;
import com.wwj.core.wawa.service.IPlayerAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/11/3.
 */
@Service
public class PlayerAddressServiceImpl implements IPlayerAddressService {

    @Resource
    private PlayerAddressMapper playerAddressMapper;

    @Override
    public void addPlayerAddress(PlayerAddress address) {
        List<PlayerAddress> addressList = playerAddressMapper.selectPlayerAddressesByPlayerId(address.getPlayerId());
        if(address.getIsDefault() == 1) {
            if(addressList != null && !addressList.isEmpty()) {
                for (PlayerAddress temp : addressList) {
                    if (temp.getIsDefault() == 1) {
                        temp.setIsDefault(0);
                        playerAddressMapper.updatePlayerAddress(temp);
                    }
                }
            }
        } else {
            if(addressList == null || addressList.isEmpty()) {
                address.setIsDefault(1);
            }
        }
        playerAddressMapper.insertPlayerAddress(address);
    }

    @Override
    public void modifyPlayerAddress(PlayerAddress address) {
        PlayerAddress old = playerAddressMapper.selectPlayerAddressById(address.getId());
        if(old.getIsDefault() != address.getIsDefault() && address.getIsDefault() == 1) {
            List<PlayerAddress> addressList = playerAddressMapper.selectPlayerAddressesByPlayerId(address.getPlayerId());
            for(PlayerAddress temp : addressList) {
                if(temp.getIsDefault() == 1 && !temp.getId().equals(address.getId())) {
                    temp.setIsDefault(0);
                    playerAddressMapper.updatePlayerAddress(temp);
                }
            }
        }
        playerAddressMapper.updatePlayerAddress(address);
    }

    @Override
    public PlayerAddress findById(Integer id) {
        return playerAddressMapper.selectPlayerAddressById(id);
    }

    @Override
    public void remove(Integer id) {
        PlayerAddress playerAddress = playerAddressMapper.selectPlayerAddressById(id);
        if(playerAddress.getIsDefault() == 1) {
            //选择第一个作为默认地址
            List<PlayerAddress> addressList = playerAddressMapper.selectPlayerAddressesByPlayerId(playerAddress.getPlayerId());
            for(PlayerAddress temp : addressList) {
                if(!temp.getId().equals(playerAddress.getId())) {
                    temp.setIsDefault(1);
                    playerAddressMapper.updatePlayerAddress(temp);
                    break;
                }
            }
        }
        playerAddressMapper.deletePlayerAddressById(id);
    }

    @Override
    public List<PlayerAddress> selectPlayerAddressesByPlayerId(int playerId) {
        return playerAddressMapper.selectPlayerAddressesByPlayerId(playerId);
    }
}
